//
//  TravelnotesViewController.m
//  SoutheastAsia
//
//  Created by rimi on 16/1/16.
//  Copyright © 2016年 zlj. All rights reserved.
//

#import "TravelnotesViewController.h"
#import "UserModel.h"
#import "CustomLoginViewController.h"

@interface TravelnotesViewController ()

@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) UIButton *collectionButton;

@property (nonatomic, strong) UIView *titleView;

@property (nonatomic, strong) NSMutableArray *titles;

@end

@implementation TravelnotesViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *nameString = [userDefaults objectForKey:@"userName"];
    if (nameString) {
        NSMutableArray *array = [userDefaults objectForKey:nameString];
        for (NSDictionary *dic in array) {
            if ([dic[@"noteTitle"] isEqualToString:self.noteTitle]) {
                self.collectionButton.selected = YES;
                break;
            } else {
                self.collectionButton.selected = NO;
            }
        }
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 加载 url
    NSURL *url = [NSURL URLWithString:_urlString];
    NSURLRequest *requset = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:requset];
    [self.view addSubview:self.webView];
    
    [self.titleView addSubview:self.backButton];
    [self.titleView addSubview:self.collectionButton];
    [self.view addSubview:self.titleView];
}

- (void)backButtonTaped:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -- action/callback

- (void)collectionButtonTaped:(UIButton *)sender {
    if ([self checkLoginStatus]) {
        // 收藏游记
        if (sender.selected == NO) {
            [self notesCollection];
            // 收藏成功提示框
            [self collectionSuccess];
            self.collectionButton.selected = YES;
        } else {
            // 删除收藏
            [self deleteNotesCollection];
            // 删除收藏成功提示框
            [self deleteCollectionSuccess];
            self.collectionButton.selected = NO;
        }
        
    } else {
        // 登录提示框
        [self loginFirst];
    }
}

#pragma mark -- private methods

// 检查登录状态
- (BOOL)checkLoginStatus {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *isLogin = [userDefaults objectForKey:@"isLogin"];

    if ([isLogin isEqualToString:@"yes"]) {
        return YES;
    } else {
        return NO;
    }
}

// 收藏游记
- (void)notesCollection {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *nameString = [userDefaults objectForKey:@"userName"];
    NSMutableArray *array = [userDefaults objectForKey:nameString];
    self.titles = [NSMutableArray arrayWithArray:array];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:self.noteTitle forKey:@"noteTitle"];
    [dic setObject:self.urlString forKey:@"urlString"];
    [self.titles addObject:dic];
    [userDefaults setObject:self.titles forKey:nameString];
    [userDefaults synchronize];
}

- (void)collectionSuccess {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [self presentViewController:alertController animated:YES completion:nil];
    [alertController addAction:sureAction];
}

// 删除收藏的游记
- (void)deleteNotesCollection {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *nameString = [userDefaults objectForKey:@"userName"];
    NSMutableArray *array = [userDefaults objectForKey:nameString];
    self.titles = [NSMutableArray arrayWithArray:array];
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    for (NSMutableDictionary *dic in array) {
        if ([dic[@"noteTitle"] isEqualToString:self.noteTitle]) {
            dictionary = dic;
        }
    }
    [self.titles removeObject:dictionary];
    [userDefaults setObject:self.titles forKey:nameString];
    [userDefaults synchronize];
}

- (void)deleteCollectionSuccess {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"取消收藏成功" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [self presentViewController:alertController animated:YES completion:nil];
    [alertController addAction:sureAction];
}

// 跳转登录界面
- (void)loginFirst {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请先登录" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        CustomLoginViewController *customLoginVC = [[CustomLoginViewController alloc] init];
        [self presentViewController:customLoginVC animated:YES completion:nil];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:nil];
    [self presentViewController:alertController animated:YES completion:nil];
    [alertController addAction:sureAction];
    [alertController addAction:cancelAction];
}

#pragma mark -- getter

- (UIView *)titleView {
    if (!_titleView) {
        _titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 80)];
        _titleView.backgroundColor = [UIColor whiteColor];
    }
    return _titleView;
}

- (UIWebView *)webView {
    if (!_webView) {
        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) + 80)];
        _webView.backgroundColor = [UIColor whiteColor];
        
    }
    return _webView;
}

- (UIButton *)backButton {
    if (!_backButton) {
        _backButton = ({
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.frame = CGRectMake(10, 30, 60, 40);
            [button setImage:[UIImage imageNamed:@"返回按钮.png"] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(backButtonTaped:) forControlEvents:UIControlEventTouchUpInside];
            button;
        });
    }
    return _backButton;
}

- (UIButton *)collectionButton {
    if (!_collectionButton) {
        _collectionButton = ({
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(CGRectGetWidth(self.view.bounds) - 45, 35, 30, 30);
            [button setBackgroundImage:[UIImage imageNamed:@"收藏.png"] forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"收藏选中.png"] forState:UIControlStateSelected];
            [button addTarget:self action:@selector(collectionButtonTaped:) forControlEvents:UIControlEventTouchUpInside];
            button;
        });
    }
    return _collectionButton;
}
@end
